package static

import (
	"embed"
	"net/http"

	"github.com/gorilla/mux"
)

const (
	// DefaultPath url prefix of pprof
	DefaultPath = "/static"
)

type option struct {
	embed      *embed.FS
	path       string
	staticPath string
}
type Options func(*option)

func WithPath(path string) Options {
	return func(o *option) {
		o.path = path
	}
}

func WithStaticPath(staticPath string) Options {
	return func(o *option) {
		o.staticPath = staticPath
	}
}

func WithEmbed(embed *embed.FS) Options {
	return func(o *option) {
		o.embed = embed
	}
}

func NewHandler(opts ...Options) http.Handler {
	opt := option{
		path: DefaultPath,
	}
	for _, o := range opts {
		o(&opt)
	}
	router := mux.NewRouter()
	if opt.embed != nil {
		router.PathPrefix(opt.path).Handler(http.FileServer(http.FS(opt.embed)))
	} else if opt.staticPath != "" {
		router.PathPrefix(opt.path).Handler(http.StripPrefix(opt.path, http.FileServer(http.Dir(opt.staticPath))))
	} else {
		panic("no static file")
	}
	return router
}
